package com.guangwei.arithmetictest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

public class ScoreResultActivity extends AppCompatActivity {
    private final static String TAG = "Score Result";

    TextView tvScore, tvTimeStamp, tvDetail;

    private String mScore, mTimeStamp;

    private static final String FILE_FOLDER =
            Environment.getExternalStorageDirectory().getAbsolutePath()
                    + File.separator + "ArithmeticTest" + File.separator + "data";
    private static final String SUMMARY_CSV = "summary.csv";
    private static final String DETAIL_CSV = "detail.csv";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score_result);

        Intent intent = getIntent();
        Bundle bd = intent.getExtras();
        mScore = bd.getString("score");
        mTimeStamp = bd.getString("timeStamp");
        Log.i(TAG, String.format("mScore=%s,  mTimeStamp=%s,", mScore, mTimeStamp));

        getView();
        initView();
        UpdateDetailResultThread updateDetailResultThread = new UpdateDetailResultThread(FILE_FOLDER, DETAIL_CSV);
        updateDetailResultThread.run();

    }

    private void getView() {
        tvScore = findViewById(R.id.tv_summary);
        tvTimeStamp = findViewById(R.id.tv_time_stamp);
        tvDetail = findViewById(R.id.tv_detail);
        tvDetail.setMovementMethod(ScrollingMovementMethod.getInstance());
    }

    private void initView() {
        tvScore.setText(mScore);
        tvTimeStamp.setText(mTimeStamp);
    }

    class UpdateDetailResultThread extends Thread {
        String fileName;
        String folder;
        public UpdateDetailResultThread(String folder, String fileName) {
            this.folder = folder;
            this.fileName = fileName;
        }
        @Override
        public void run() {
            super.run();
            File inFile = new File(folder + File.separator + fileName);
            final StringBuilder cSb = new StringBuilder();
            String inString;
            try {
                BufferedReader reader = new BufferedReader(new FileReader(inFile));
                int count = 0;
                while ((inString = reader.readLine()) != null) {
                    String[] strList = inString.split(",");
                    if (strList.length > 5) {
                        if (strList[0].equals(mTimeStamp)) {
                            count++;
                            String questionResult = strList[1] + "=" + strList[3];
                            String status = strList[4];
                            cSb.append(questionResult);
                            if (status.equals("Pass")) {
                                cSb.append("✔");
                            } else {
                                cSb.append("✘");
                            }
                            int tabSize = 20;
                            int spaceNumber = tabSize - questionResult.length();
                            for (int i = 0; i < spaceNumber; i++) cSb.append("  ");
                            if (count % 2 == 0) cSb.append("\n");
                        }
                    }
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    //mCSVTv.setText(cSb.toString());// 显示
                    tvDetail.setText(cSb.toString());
                    Log.i(TAG, cSb.toString());
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
